<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/5
 * Time: 14:03
 */


namespace App\Http\Controllers\Dynamic;

use App\Http\Controllers\ApiController;
use App\LogicService\Contracts\BloodGlucoseContrastLogicContract;
use App\User;
use Illuminate\Http\Request;

class BloodGlucoseContrastController extends ApiController
{

    protected $request;

    protected $user;

    protected $bloodGlucoseContrast;

    public function __construct(Request $request,User $user,BloodGlucoseContrastLogicContract $bloodGlucoseContrast
                               )
    {
        parent::__construct();
        $this->request = $request;
        $this->user = $user;
        $this->bloodGlucoseContrast = $bloodGlucoseContrast;

    }


    public function store(){
        $this->validate($this->request, [
            'user_id' => 'required|integer',
            'contrast' => 'array',
        ]);

        $user_id = $this->request->input('user_id');
        $user = $this->user
            ->withoutGlobalScopes()
            ->select('id', 'account')
            ->find($user_id);
        $account = $user->account;

        $contrast_array = $this->request->input('contrast');


        $contrast_data_result = array();
        if (count($contrast_array)) {
            foreach ($contrast_array as $key => $value) {
                $contrast = $this->bloodGlucoseContrast->update($account, $value);
                $contrast_data_result[] = $contrast;
            }
        }

        return $this->response($contrast_data_result);

    }

    public function getList()
    {

        $this->validate($this->request, [
            'user_id' => 'required|integer',
        ]);
        $user_id = $this->request->input('user_id');
        $user = $this->user
            ->withoutGlobalScopes()
            ->select('id', 'account')
            ->find($user_id);
        $account = $user->account;

        $contrast_data_result = $this->bloodGlucoseContrast->getList($account);

        return $this->response($contrast_data_result);
    }
}
